﻿
@{
    ViewBag.Title = "MenuEdit";
    Layout = "~/Views/Shared/layoutCommon.cshtml";
}


@model OF.Component.Model.WxOfficialAccountMenuInfo

@section style{
    <style>
        .form-horizontal .control-label {
            width: 30%;
            float: left;
            text-align: right;
        }

        .col-sm-3 {
            width: 70%;
            float: left;
        }
    </style>
}
<form id="form1" class="form-horizontal">
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box-body">

                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">父级：</label>
                        <div class="col-sm-3">
                            <select class="form-control" style="width:300px;" name="selParentId" id="selParentId"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">名称：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" value="@Model.MenuName" style="width:300px;" name="txtName" maxlength="7" id="txtName" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">类型：</label>
                        <div class="col-sm-3">
                            <select class="form-control" style="width:300px;" name="selType" id="selType"></select>
                        </div>
                    </div>
                    <div class="form-group" style="display:none;" id="divKey">
                        <label class="col-sm-2 control-label" for="name">菜单KEY值：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" value="@Model.MenuKey" style="width:300px;" name="txtMenuKey" maxlength="16" id="txtMenuKey" />
                        </div>
                    </div>
                    <div class="form-group" style="display:none;" id="divAppId">
                        <label class="col-sm-2 control-label" for="name">AppId：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" value="@Model.MenuAppId" style="width:300px;" name="txtAppId" maxlength="30" id="txtAppId" />
                        </div>
                    </div>
                    <div class="form-group" style="display:none;" id="divUrl">
                        <label class="col-sm-2 control-label" for="name">网页链接：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" value="@Model.MenuUrl" style="width:300px;" name="txtURL" maxlength="255" id="txtURL" />
                        </div>
                    </div>
                    <div class="form-group" style="display:none;" id="divMenuPagePath">
                        <label class="col-sm-2 control-label" for="name">小程序页面路径：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" value="@Model.MenuPagePath" style="width:300px;" name="txtMenuPagePath" maxlength="255" id="txtMenuPagePath" />
                        </div>
                    </div>
                 
                </div>
                <div class="box-footer text-center">
                    <button class="btn btn-primary" type="button" id="btnSave">确 定 </button>
                    <button class="btn" type="button" id="cancel">取 消 </button>
                    <input type="hidden" id="MenuID" name="MenuID" value="@Model.MenuId" />
                </div>


            </div>

        </div>
        <!-- /.row -->
    </section>
  
</form>
@section scriptsBottom{
    <script type="text/javascript">
    $(document).ready(function () {
     

        initParentMenu();
        initFormValidate();
        initMenuType();
        initBindMenuType();
        initPageMenuTypeShow();
        $("#cancel").click(function () {
            BMCW.closeTopLayer();
        });
        $("#btnSave").click(function () {
            save();
        });
    });

    //父级菜单
    function initParentMenu() {
        var pMenuId = @Model.MenuParentId;
        var pId = @ViewBag.Pid;
        var mid = @Model.MenuId;
        $("#selParentId").append("<option value='0'>一级菜单</option>");
        if((mid > 0 && pMenuId == 0)||pId > 0||(mid == 0 && pMenuId == 0 && pId == 0)){
            $("#selParentId").attr("disabled", "disabled");
        }
        if (pMenuId == 0&& pId==0) {
            return;
        }
        var url = '@Url.Action("GetParentMenuList", "Wx")';
        BMCW.ajax({
            url: url,
            type: "GET",
            success: function (o) {
                if (o.status == 0) {
                    $.each(o.data, function (i) {
                        var sel = "";
                        if(pId > 0)
                        {
                            if (o.data[i].MenuId==pId) {
                                sel = "selected='true'";
                            }
                        }else{
                            if (o.data[i].MenuId == pMenuId) {
                                sel = "selected='true'";
                            }
                        }
                        $("#selParentId").append("<option value='" + o.data[i].MenuId + "' "+sel+">" + o.data[i].MenuName + "</option>");
                    });
                }
            }
        });
    }

        //菜单类型
        function initMenuType() {
            var pMenuId = @Model.MenuParentId;
            var pId = @ViewBag.Pid;
            var mid = @Model.MenuId;
            var typeId = @Model.MenuType;
            $("#selType").append("<option value=''>请选择类型</option>");
         
            var url = '@Url.Action("GetMenuTypeList", "Wx")';
            BMCW.ajax({
                url: url,
                type: "GET",
                success: function (o) {
                    if (o.status == 0) {
                        $.each(o.data, function (i) {
                            var sel = "";
                                if (i==typeId) {
                                    sel = "selected='true'";
                                }
                            $("#selType").append("<option value='" + i + "' "+sel+">" + o.data[i] + "</option>");
                        });
                    }
                }
            });
        }

        function initBindMenuType()
        {
            $("#selType").change(function(){
                var typeId = $(this).val();
                MenuTypeChangeEvent(typeId);
            });
        }

        function initPageMenuTypeShow()
        {
            var typeId = @Model.MenuType;
            if (typeId>0) {
                MenuTypeChangeEvent(typeId);
            }
            
        }

        function MenuTypeChangeEvent(typeId)
        {

                if (typeId=='') {
                    $("#divUrl").hide();
                    $("#divAppId").hide();
                    $("#divMenuPagePath").hide();
                    $("#divKey").hide();
                    RemoveFormValidate("txtURL");
                    RemoveFormValidate("txtAppId");
                    RemoveFormValidate("txtMenuPagePath");
                    RemoveFormValidate("txtMenuKey");
                }else if (typeId==2) {
                    $("#divUrl").show();
                    $("#divAppId").hide();
                    $("#divMenuPagePath").hide();
                    $("#divKey").hide();
                    AddFormValidate("txtURL","URL不能为空");
                    RemoveFormValidate("txtAppId");
                    RemoveFormValidate("txtMenuPagePath");
                    RemoveFormValidate("txtMenuKey");
                }else if(typeId==11)
                {
                    $("#divUrl").show();
                    $("#divAppId").show();
                    $("#divMenuPagePath").show();
                    $("#divKey").hide();
                    AddFormValidate("txtURL","URL不能为空");
                    AddFormValidate("txtAppId",'小程序AppId不能为空');
                    AddFormValidate("txtMenuPagePath",'小程序的页面路径不能为空');
                    RemoveFormValidate("txtMenuKey");
                }else
                {
                    $("#divKey").show();
                    $("#divUrl").hide();
                    $("#divAppId").hide();
                    $("#divMenuPagePath").hide();
                    RemoveFormValidate("txtURL");
                    RemoveFormValidate("txtAppId");
                    RemoveFormValidate("txtMenuPagePath");
                    AddFormValidate("txtMenuKey",'菜单KEY值不能为空');
                }
        
        }

        function AddFormValidate(fieldName,msg){
            $("#form1").bootstrapValidator("addField", fieldName, {
                validators: {
                    notEmpty: {
                        message: msg
                    }
                }
            });
        }

        function RemoveFormValidate(fieldName){
            $("form").bootstrapValidator('removeField',fieldName);
        }


    //保存
    function save()
    {
        var bootstrapValidator = $("#form1").data('bootstrapValidator');
        bootstrapValidator.validate();

        if (bootstrapValidator.isValid()) {
            var url = '@Url.Action("SaveMenu", "Wx")';
            var arrData = $("#form1").serializeArray();
            var data = {};
            for (var i = 0; i < arrData.length; i++) {
                data[arrData[i].name] = arrData[i].value;
            }
            if (data.selParentId==null) {
                data["selParentId"] = $("#selParentId").val();
            }
            BMCW.ajax({
                url: url,
                type: "POST",
                data: data,
                success: function (o) {
                    if (o.status == 0) {
                        BMCW.msg("保存成功",function(){
                            BMCW.getTopSelectedWin().initData();
                        });
                    } else {
                        BMCW.alert("保存失败");
                    }
                }
            });
        }
    }

    //验证
    function initFormValidate() {
        $("#form1").bootstrapValidator({
            submitHandler: function (valiadtor, loginForm, submitButton) {
                valiadtor.defaultSubmit();
            },
            fields: {
                txtName: {
                    validators: {
                        notEmpty:{
                            message: '菜单名称不能为空'
                        },
                        stringLength: {
                            min: 1,
                            max: 7,
                            message: '菜单名称长度必须在1到7之间'
                        }
                    }
                },
                selType: {
                    validators: {
                        notEmpty: {
                            message: '请选择所属类别'
                        }
                    }
                }
            }
        });
    }

    </script>
}




